package cn.zldemo.common;

import android.animation.Animator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.Button;
import cn.zldemo.R;
import cn.zldemo.util.SetOnClickUtil;
import cn.zldemo.util.Utils;
import cn.zldemo.util.ViewMapping;
import cn.zldemo.util.ViewMappingUtil;

@ViewMapping(id = R.layout.activity_card_view)
public class CardViewActivity extends AppCompatActivity implements View.OnClickListener {
	@ViewMapping(id = R.id.card)
	CardView cardView;
	@ViewMapping(id = R.id.btn1)
	Button btn1;
	@ViewMapping(id = R.id.btn2)
	Button btn2;
	@ViewMapping(id = R.id.btn3)
	Button btn3;
	@ViewMapping(id = R.id.btn4)
	Button btn4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewMappingUtil.mapView(this, this);
		SetOnClickUtil.registerOnClick(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_card_view, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		// noinspection SimplifiableIfStatement
		if (id == R.id.action_settings) {
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn1:
			cardView.setUseCompatPadding(false);
			cardView.setContentPadding(0, 0, 0, 0);
			cardView.setMaxCardElevation(5);
			break;
		case R.id.btn2:
			cardView.setUseCompatPadding(true);
			cardView.setContentPadding(5, 5, 5, 5);
			cardView.setMaxCardElevation(10);
			break;
		case R.id.btn4:
			if (Utils.hasLollipop()) {
				View myView = findViewById(R.id.reveal_view);
				int cx = (myView.getLeft() + myView.getRight()) / 2;
				int cy = (myView.getTop() + myView.getBottom()) / 2;
				int finalRadius = Math.max(myView.getWidth(), myView.getHeight());
				Animator anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
				myView.setVisibility(View.VISIBLE);
				anim.start();
			}
			break;
		}
	}
}
